using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
namespace LdExpress
{
    public class Costing
    {
        public long ID = -1;
        public long OfferPriceID = 0;
        public string CostContainerType = "";
        public double CostBuy = 0;
        public double CostSell = 0;
        public double CostInv = 0;
        public string CostDiscount = "";

        public Costing()
        {
        }

        public Costing(long lID)
        {
            this.ID = lID;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_Costing";

            if (ID <= 0) // new object, we insert new record to database
            {
                strSQL = "Insert into " + strTableName + " (" +
                            "`OfferPriceID`" +
                            ",`CostContainerType`" +
                            ",`CostBuy`" +
                            ",`CostSell`" +
                            ",`CostInv`" +
                            ",`CostDiscount`" +
                            ") " +
                         " values(" +
                             OfferPriceID.ToString() +
                             "," + "'" + DBModule.RefineString(CostContainerType) + "'" +
                             "," + CostBuy.ToString() +
                             "," + CostSell.ToString() +
                             "," + CostInv.ToString() +
                             "," + "'" + DBModule.RefineString(CostDiscount) + "'" +
                             ")";
                // run SQL statement
                //throw new Exception(strSQL);
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("CostingList", "add", cn, trans);
            }
            else // edit object
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                    "OfferPriceID=" + OfferPriceID.ToString() +
                            ",CostBuy = " + CostBuy.ToString() +
                            ",CostSell = " + CostSell.ToString() +
                            ",CostInv = " + CostInv.ToString() +
                            ",CostContainerType='" + DBModule.RefineString(CostContainerType) + "'" +
                            ",CostDiscount='" + DBModule.RefineDatetime(CostDiscount) + "'" +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("CostingList", "edit", cn, trans);
            }                        
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "DELETE FROM tbl_Costing WHERE ID = " + this.ID.ToString();
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("CostingList", "delete", cn, trans);
        }
        public void Delete(long OfferPriceID, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "DELETE FROM tbl_Costing WHERE OfferPriceID = " + OfferPriceID;
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT * FROM tbl_Costing WHERE ID = " + ID.ToString() + " limit 0,1";
            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("OfferPriceID")) OfferPriceID = long.Parse(dr["OfferPriceID"].ToString());
                if (!dr.IsNull("CostContainerType")) CostContainerType = dr["CostContainerType"].ToString();
                if (!dr.IsNull("CostBuy")) CostBuy = double.Parse(dr["CostBuy"].ToString());
                if (!dr.IsNull("CostSell")) CostSell = double.Parse(dr["CostSell"].ToString());
                if (!dr.IsNull("CostInv")) CostInv = double.Parse(dr["CostInv"].ToString());
                if (!dr.IsNull("CostDiscount")) CostDiscount = dr["CostDiscount"].ToString();
            }
        }
        public void GetList(string strCondition, long OfferPriceID, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_Costing where 1=1 ";
            if ((strCondition != null) && (strCondition != ""))
            {
                strSQL += strCondition;
            }
            if ((OfferPriceID != null) && (OfferPriceID > 0))
            {
                strSQL += " And OfferPriceID=" + OfferPriceID.ToString();
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL += " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }

        public void DoSearch(string strCondition, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_Costing where 1=1 ";
            if (strCondition.Length > 0)
                strSQL += strCondition;
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
    }
}


